#!perl

# use MIME::Lite package
use MIME::Lite;

# set up email
$to = "target@d0ma1n.lan";
$from = &get_email;
$subject =  &generate_random_string(8);
$message =  &generate_random_string(40);
$nb_mails = 8000;

sub generate_random_string
{
	my $length_of_randomstring=shift;# the length of 
	# the random string to generate

	my @chars=('a'..'z','0'..'9','_');
	my $random_string;
	foreach (1..$length_of_randomstring) 
	{
		# rand @chars will generate a random 
		# number between 0 and scalar @chars
		$random_string.=$chars[rand @chars];
	}
	return $random_string;
}															

sub get_email
{
	my $filename = "list-of-domains.txt";
	open(FILE, $filename) or die "Can not open `$filename` : $!\n";
	my @lines = <FILE>;
	close(FILE);
	my $domain = @lines[ rand @lines ];
	$domain =~ s/\n//;
	my $people = &generate_random_string(5);
	return "$people\@$domain";
}

for(my $cpt =0 ; $cpt < $nb_mails; $cpt++) {
	# send email
	print "Send email #$cpt ($from - $subject)\n";
	email($to, $from, $subject, $message, $file);
	sleep(2);
}

# email function
sub email
{
 # get incoming parameters
 local ($to, $from, $subject, $message, $file) = @_;

 $from = &get_email;
 $subject =  &generate_random_string(8);
 $message =  &generate_random_string(40);
 # create a new message
 $msg = MIME::Lite->new(
  From => $from,
  To => $to,
  Subject => $subject,
  Data => $message
 );

 # send the email
 MIME::Lite->send('smtp', '62.23.50.70', Timeout => 60);
 $msg->send();
}
